package com.example.mall.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.mall.domain.UserPoints;
import com.example.mall.dto.UserPointsDto;
import com.example.mall.mapper.UserPointsMapper;
import com.example.mall.query.PageDTO;
import com.example.mall.query.UserPointsQuery;
import com.example.mall.service.IUserPointsService;

import com.example.mall.util.AssertUtil;
import com.example.mall.util.CodeGenerateUtils;
import com.example.mall.util.CopyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 用户积分表 服务实现类
 * </p>
 *
 * @author whale
 * @since 2025-05-09
 */
@Service
public class UserPointsServiceImpl extends ServiceImpl<UserPointsMapper, UserPoints> implements IUserPointsService {

    @Autowired
    private UserPointsMapper userPointsMapper;

    @Override
    public PageDTO<UserPointsDto> queryPage(UserPointsQuery userPointsQuery) {
        if (userPointsQuery == null) {
            userPointsQuery = new UserPointsQuery();
        }
        Page<UserPoints> page = userPointsQuery.toMpPageByDefaultCreateTimeAsc();
        Page<UserPoints> cartPage = lambdaQuery()
                .like(userPointsQuery.getPointId()!= null, UserPoints::getPointId, userPointsQuery.getPointId())
                .like(userPointsQuery.getUserId()!= null, UserPoints::getUserId, userPointsQuery.getUserId())
                .like(userPointsQuery.getPoints()!= null, UserPoints::getPoints, userPointsQuery.getPoints())
                .like(userPointsQuery.getDescription()!= null, UserPoints::getDescription, userPointsQuery.getDescription())
                .orderByDesc(UserPoints::getUpdateDate)
                .page(page);
        return PageDTO.of(cartPage, UserPointsDto.class);
    }

    @Override
    public UserPoints queryUserPointsById(Long pointId) {
        AssertUtil.isNotNull(pointId, "积分ID不能为空");
        return userPointsMapper.selectById(pointId);
    }

    @Override
    public List<UserPoints> queryUserPointsList() {
        return this.list();
    }

    @Override
    public void insert(UserPointsDto userPointsDto) {
        UserPoints userPoints = new UserPoints();
        CopyUtils.copyProperties(userPointsDto, userPoints);
        userPoints.setPointId(CodeGenerateUtils.generateUniqueId());
        userPoints.setCreateDate(new Date());
        userPoints.setUpdateDate(new Date());
        userPointsMapper.insert(userPoints);
    }

    @Override
    public void update(UserPointsDto userPointsDto) {
        AssertUtil.isNotNull(userPointsDto.getPointId(), "积分ID不能为空");
        UserPoints userPoints = userPointsMapper.selectById(userPointsDto.getPointId());
        AssertUtil.isNotNull(userPoints, "积分信息不存在");
        CopyUtils.copyProperties(userPointsDto, userPoints);
        userPoints.setUpdateDate(new Date());
        userPointsMapper.updateById(userPoints);
    }

    @Override
    public void delete(Long pointId) {
        AssertUtil.isNotNull(pointId, "积分ID不能为空");
        UserPoints userPoints = userPointsMapper.selectById(pointId);
        AssertUtil.isNotNull(userPoints, "积分信息不存在");
        userPointsMapper.deleteById(pointId);
    }

    @Override
    public void deleteBatch(List<Long> pointIds) {
        AssertUtil.isNotNull(pointIds, "积分ID列表不能为空");
        List<UserPoints> userPoints = userPointsMapper.selectBatchIds(pointIds);
        AssertUtil.isNotNull(userPoints, "积分信息不存在");
        userPointsMapper.deleteBatchIds(pointIds);
    }
}
